package com.alesj.newsfeed.web.renderer;

import com.alesj.newsfeed.data.Contact;
import com.alesj.newsfeed.data.ReporterContact;
import com.generalynx.common.web.tags.AbstractSelectRenderer;
import com.generalynx.common.web.tags.AwareSelectRenderer;
import org.hibernate.mapping.Property;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ContactEmailSelectRenderer extends AbstractSelectRenderer implements AwareSelectRenderer {

    private Contact contact;

    public void setOwnerAndPropertyValue(Object owner, Property property, Object value) {
        if (!(owner instanceof Contact))
            throw new IllegalArgumentException("Renderer applies only to Contact owner: " + owner);
        contact = (Contact) owner;
    }

    public Collection getSelectCollection() {
        List list = new ArrayList();
        if (contact.getEmail() != null) list.add(contact.getEmail());
        if (contact instanceof ReporterContact && ((ReporterContact)contact).getEmail2() != null) {
            list.add(((ReporterContact)contact).getEmail2());
        }
        return list;
    }

}
